// pages/goodsDetail/goodsDetail.js
const util = require("../../utils/util.js")
const api = require("../../utils/net.js")
const wxUtils = require("../../utils/wxUtils.js")
const session = require("../../utils/session.js")
const {
  $Toast
} = require('../../dist/base/index')
const WxParse = require('../../wxParse/wxParse.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showShare: false,
    sharedParams: null,
    isLoading: true,
    goodsArray: new Array(),
    discoveryId: 1,
    discovery: null,
    xToast: {
      show: false
    }
  },
  showXToast: function(toast) {
    toast.show = true
    this.setData({
      xToast: toast
    })
    let that = this
    setTimeout(() => {
      that.setData({
        ['xToast.show']: false
      })
      if (toast.callBack) toast.callBack()
    }, 1500)
  },
  hideLoading: function() {

    this.setData({
      isLoading: false
    })
  },
  showLoading: function() {
    this.setData({
      isLoading: true
    })
  },
  loadData: function() {
    let that = this
    api.get(`discovery/${this.data.discoveryId}`, {}, {
      onNext: (res) => {
        that.setData({
          discovery: res
        })
        var ids = res.goodsIds
        var idsArr = new Array();
        if (!util.isEmptyStr(ids)) {
          idsArr = ids.split(",");
          for (var index in idsArr) {
            that.loadProductInfo(idsArr[index]);
          }
        }

        WxParse.wxParse('articleContent', 'html', res.content, this, 5);
        this.updateNavTitle()
      },
      onError: (msg, code) => $Toast({
        content: msg,
        type: 'error'
      }),
      onCompleted: () => {
        that.hideLoading()
      }
    })
  },
  updateNavTitle: function() {
    wx.setNavigationBarTitle({
      title: this.data.discovery.title || '活动'
    })
  },
  toGoodsDetailPage: function(e) {
    let goodsId = e.currentTarget.dataset.goods_id
    if (goodsId) {
      util.toPage("../goodsDetail/goodsDetail", {
        "goodsId": goodsId
      })
    }
  },
  loadProductInfo: function(goodsId) {
    let that = this
    api.get(`goodsInfo/${goodsId}`, {
      self: !!app.globalData.userInfo.shopId
    }, {
      onNext: (res) => {

        var _goods = this.data.goodsArray;
        _goods.push(res)

        that.setData({
          goodsArray: _goods
        })


      },
      onCompleted: () => {
        that.hideLoading()
      }
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    util.decodeQueryParams(options)

    let _discoveryId = options.discoveryId
    this.setData({
      discoveryId: _discoveryId
    })

    // 阅读数+1

    this.addReadAmount();

  },

  addReadAmount: function() {
    let that = this
    api.get('discovery/modifyReadAmount', {
      "id": that.data.discoveryId
    }, {
      onNext: res => {

      },
      onCompleted: () => {

      }

    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

    session.check((msg) => {

      if (msg) {
        this.showXToast({
          title: msg,
          icon: 'no'
        })
        return
      }
      if (this.data.discovery == null) {
        this.loadData()
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },



  onShareAppMessage: function() {

    return {
      title: this.data.goods.name,
      path: `pages/goodsDetail/goodsDetail?shopId=${app.globalData.shopId}&goodsId=${this.data.goodsId}&userId=${app.globalData.userInfo.id}`,
      imageUrl: this.data.goods.picUrls[0]
    }
  },

  onOpenSharePopup: function() {
    let _shareData = this.data.goods
    if (this.data.currentProduct) {
      _shareData.profit = this.data.currentProduct.profit
    }
    this.setData({
      shareData: _shareData,
      showShare: true
    })
  },
  onCloseSharePopup: function(e) {
    this.setData({
      showShare: false
    })
  },
  preventTouchPop: function() {},
})